home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-01-01 | 3.1 KB | 138 lines | [TEXT/PJMM] |
- { Quadratic Equation Example program }
- { By Dave Kelly }
- { with mods by Dave Smith }
- { ©MacTutor, 1988 }
-
- PROGRAM quadexample;
-
- {$I-}
-
- USES
- PlotGlobals;
-
-
- FUNCTION positivecalc (a, b, check : real) : real;
- BEGIN
- positivecalc := (-b + sqrt(check)) / (2 * a);
- END;
-
- FUNCTION negativecalc (a, b, check : real) : real;
- BEGIN
- negativecalc := (-b - sqrt(check)) / (2 * a);
- END;
-
- PROCEDURE quad (a, b, c : real;
- VAR x1, x2 : real;
- VAR result : integer);
- VAR
- check : real;
- BEGIN
- result := 0;
- check := (b * b) - (4 * a * c);
- IF result = 0 THEN
- BEGIN
- { Check if double root exists }
- IF check = 0 THEN
- BEGIN
- result := 2;
- x1 := positivecalc(a, b, check);
- END;
- IF check > 0 THEN
- BEGIN
- result := 1;
- x1 := positivecalc(a, b, check);
- x2 := negativecalc(a, b, check);
- END;
- { Check if root is complex }
- IF check < 0 THEN
- BEGIN
- result := 3;
- check := -check;
- x1 := positivecalc(a, b, check);
- x2 := negativecalc(a, b, check);
- END;
- END;
- END;
-
- PROCEDURE plotit (a, b, c, step : real;
- xscale, yscale : integer);
- VAR
- hPos, vPos, hor, ver : integer;
- x, y : real;
- grafwrect, localrect : rect;
- BEGIN
- showdrawing;
- getdrawingrect(grafwrect);
- hor := (grafwrect.right - grafwrect.left) DIV 2;
- ver := (grafwrect.bottom - grafwrect.top) DIV 2;
- setrect(localrect, 0, 0, hor + hor, ver + ver);
- EraseRect(localrect);
- moveto(0, ver);
- line(hor + hor, 0);
- moveto(hor, 0);
- line(0, ver + ver);
- x := -xscale / 2;
- y := a * x * x + (b * x) + c;
- hPos := integer(round(x * hor * 2 / xscale + hor));
- vPos := integer(round(-y * ver * 2 / yscale + ver));
- moveto(hPos, vPos);
- PenNormal;
- REPEAT
- x := x + step;
- y := a * x * x + (b * x) + c;
- hPos := integer(round(x * hor * 2 / xscale + hor));
- vPos := integer(round(-y * ver * 2 / yscale + ver));
- LineTo(hPos, vPos);
- UNTIL x >= xscale / 2;
- END;
-
- FUNCTION solveit : integer;
- BEGIN
- showtext;
- write('Enter value for variable a ( 0 to quit) :');
- readln(a);
- IF a <> 0 THEN
- BEGIN
- write('Enter value for variable b :');
- readln(b);
- write('Enter value for variable c :');
- readln(c);
- quad(a, b, c, x1, x2, result);
- writeln('a= ', a : 10 : 4, ' b= ', b : 10 : 4, ' c=', c : 10 : 4);
- END
- ELSE
- result := -1;
- IF result = -1 THEN
- writeln('Data is not a quadratic equation or is illegal')
- ELSE IF result = 3 THEN
- BEGIN
- writeln('Root is complex!');
- writeln('x1= ', x1 : 10 : 4, ' x2= ', x2 : 10 : 4);
- END
- ELSE IF result = 2 THEN
- writeln('Double Root = ', x1 : 10 : 4)
- ELSE
- BEGIN
- writeln('x1= ', x1 : 10 : 4, ' x2= ', x2 : 10 : 4);
- writeln('');
- END;
- solveit := result;
- END;
-
- { Main Program}
-
- BEGIN
- step := 0.005;
- xscale := 20;
- yscale := 400;
- setrect(WindowRect, 25, 50, 400, 250);
- setrect(PlotWindowRect, 100, 200, 600, 450);
- settextrect(WindowRect);
- setdrawingrect(PlotWindowRect);
- REPEAT
- result := solveit;
- IF result <> -1 THEN
- plotit(a, b, c, step, xscale, yscale);
- UNTIL result = -1;
- writeln('Program is terminating');
- END.